/*
* 文件名：WebUtils
* 版权：Copyright @ 2017 lottery-common-lang All Rights Reserved.
* 描述：
* 修改人：lottery
* 修改时间：2017年10月15日 下午2:49:30
* 修改内容：〈修改内容〉
*/
package com.lemon.core.common.web.response;

import com.lemon.core.common.lang.util.Assert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @desc: WebUtils
 * @author: 柠檬的柠丶
 * @createTime: 2017年10月15日 下午2:49:30
 * @history:
 * @version: v1.0
 */
public class WebUtils {
	
	/**
	 * text/html
	 */
	public static final String CONTENTTYPE_TEXTHTML = "text/html";
	
	/**
	 * text/json
	 */
	public static final String CONTENTTYPE_TEXTJSON = "text/json";
	
	/**
	 * text/xml
	 */
	public static final String CONTENTTYPE_TEXTXML = "text/xml";
	
	/**
	 * GBK
	 */
	public static final String CONTENT_CHARSET_GBK = "GBK";
	
	/**
	 * GB2312
	 */
	public static final String CONTENT_CHARSET_GB2312 = "GB2312";
	
	/**
	 * UTF-8
	 */
	public static final String CONTENT_CHARSET_UTF8 = "UTF-8";
	
	/**
	 * application/json
	 */
	public static final String APPLICATIONTYPE_JSON = "application/json";
	
	
	/**
	*
	* @author: lottery
	* @createTime: 2017年10月15日 下午2:51:02
	* @history:
	* @param request
	* @param name
	* @param clazz
	* @return T
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getOrCreateSessionAttribute(HttpServletRequest request, String name, Class<T> clazz) {
		return (T) getOrCreateSessionAttribute(request.getSession(), name, clazz);
	}
	
	/**
	* 重定向返回数据
	* @author: lottery
	* @createTime: 2017年10月15日 下午2:51:06
	* @history:
	* @param response		响应
	* @param contentType	响应类型
	* @param charset		字符集
	* @param s void
	 */
	public static void sendDirectToClient(HttpServletResponse response, String contentType, String charset, String s) {
		String charsetPrefix = ";charset=";
		String contentHead = contentType + charsetPrefix + charset;
		response.setContentType(contentHead);
		try {
			PrintWriter writer = response.getWriter();
			writer.write(s);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static Object getOrCreateSessionAttribute(HttpSession session, String name, Class<?> clazz)
			throws IllegalArgumentException {

		Assert.notNull(session, "Session must not be null");
		Object sessionObject = session.getAttribute(name);
		if (sessionObject == null) {
			try {
				sessionObject = clazz.newInstance();
			}
			catch (InstantiationException ex) {
				throw new IllegalArgumentException(
						"Could not instantiate class [" + clazz.getName() +
								"] for session attribute '" + name + "': " + ex.getMessage());
			}
			catch (IllegalAccessException ex) {
				throw new IllegalArgumentException(
						"Could not access default constructor of class [" + clazz.getName() +
								"] for session attribute '" + name + "': " + ex.getMessage());
			}
			session.setAttribute(name, sessionObject);
		}
		return sessionObject;
	}
}
